import com.chen.common.util.time.TimeUtil;

import java.util.concurrent.*;

/**
 * @author ex_chenzy59
 * @since 2025/2/26
 * 尝试使用ForkJoinPool管理虚拟线程,未实现
 */
@Deprecated
public class 虚拟线程2 {
    public final static int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    /**
     * 得到虚拟线程池
     */
    public static ForkJoinPool getVirtualExecutor(int maxThreadSize) {
        int keepAliveTime = 30;//空闲线程存活时间
        int coreSize = Math.max(2, Math.min(CPU_COUNT - 1, 4));//核心线程数
        int maxSize = Math.max(maxThreadSize,Math.max(CPU_COUNT * 2 + 1,5000));//最大线程数
        System.out.println("coreSize:"+coreSize);
        System.out.println("CPU_COUNT:"+CPU_COUNT);
        System.out.println("最大线程数:"+maxSize);
         new ForkJoinPool(
                coreSize,
                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                null,
                 true
        );


        new ForkJoinPool(
                coreSize,
                new ForkJoinPool.ForkJoinWorkerThreadFactory() {
                    private final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

                    @Override
                    public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
                        ForkJoinWorkerThread workerThread = new ForkJoinWorkerThread(pool) {
                        };
                        var da=Thread.ofVirtual().name("ForkJoinWorkerThread").unstarted(workerThread);
                        Thread.ofVirtual().factory();
                        return null;
                    }
                },
                null,
                true, coreSize, maxSize, coreSize, null,keepAliveTime,TimeUnit.SECONDS
        );
        return null;
    }
    static class VirtualForkJoinWorkerThread extends ForkJoinWorkerThread {
        public VirtualForkJoinWorkerThread(ForkJoinPool pool) {
            super(pool);
        }
    }
//    static class WorkerThread extends ForkJoinWorkerThread {
//        WorkerThread(ForkJoinPool pool, ClassLoader contextClassLoader) {
//            super(pool);
//            this.setContextClassLoader(contextClassLoader);
//        }
//    }
    private static void processTask(String task) {
        try {
            countDownLatch.countDown();
            TimeUnit.SECONDS.sleep(1); // 模拟网络延迟
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        }
//        2025-02-26 17:57:12.678Processed task: Task 95 in thread: Thread[#32,ForkJoinPool-1-worker-3,5,main]
//        2025-02-26 17:57:29.102Processed task: Task 89 in thread: VirtualThread[#123]/runnable@ForkJoinPool-1-worker-15

        System.out.println(TimeUtil.nowStrSSS()+"Processed task: " + task + " in thread: " + Thread.currentThread());
    }
    public static int size=100;
    public static CountDownLatch countDownLatch=new CountDownLatch(size);
    public static void main(String[] args) throws InterruptedException {
        var begin=System.currentTimeMillis();

        var executor=getVirtualExecutor(10000);
        // 提交任务
        for (int i = 0; i < size; i++) {
            int finalI = i;
            executor.submit(() -> processTask("Task " + finalI));
        }
        System.out.println(TimeUtil.nowStrSSS()+"--------------------------------");
//        17:00:09.675
//        17:00:33.915
        countDownLatch.await();
        var end=System.currentTimeMillis();
        System.out.println("共耗时:"+(end-begin));
        executor.shutdown();// 关闭线程池
    }
}
